<template>
	<skeleton :skeleton="{name:'history'}" :showSkeleton="showSkeleton">
		<block slot="content">
			<noData :showErrorTip="noData">
				<view slot="content" class="container">
					<view class="list">
						<view class="list-item" v-for="(item,index) in list" :key="index" @click="analysis(index,item.examId,item.recordId,item.finished)">
							<view class="list-item-left">{{item.title}}</view>
							<view class="list-item-right" v-if="item.finished == 1"><text class="list-item-right-btn">继续答题</text></view>
							<view v-if="item.finished == 2" class="list-item-right">{{item.time}}</view>
						</view>
					</view>
					<view class="loading" v-if="noMoreData">没有更多了</view>
				</view>
			</noData>
		</block>
	</skeleton>
</template>

<script>
	import {isEmpty} from '@/utils/index';
	import ExamService from '@/services/exam/exam';
	const examService = new ExamService();
	export default {
		data() {
			return {
				list: [],
				examId: '100101',
				showSkeleton: true,
				ascription: undefined,
				examType: undefined,
				resultType:undefined,
				pages: {
					pageNum: 1, //页码
					pageSize: 20 //每页展示条数
				},
				noMoreData: false,
				curIndex:-1
			}
		},
		computed: {
			noData() {
				return !this.showSkeleton && this.list.length == 0
			}
		},
		onLoad(query) {
			//继续答题完成后，改变条目状态
			uni.$on('examFinishHistory',(payload)=>{
				console.log('examFinish  history');
				if(!isEmpty(this.list)&&!isEmpty(this.list[this.curIndex])){
					this.list[this.curIndex].finished = payload.isFinished;
					this.list[this.curIndex].recordId = payload.recordId;
				}
				this.curIndex = -1;
			});
			this.examType = query.examType;
			this.resultType = query.resultType;
			this.ascription = query.ascription;
			this.loadData(1);
		},
		onUnload() {
			uni.$off('examFinishHistory');
		},
		//下拉刷新
		onPullDownRefresh() {
			this.loadData(1);
		},
		//滑动到底部加载更多
		onReachBottom() {
			this.loadData(this.pages.pageNum + 1);
		},
		methods: {
			loadData(pageNum) {
				const param = {
					examType: this.examType,
					ascription: this.ascription,
					resultType:this.resultType,
					pageNum: pageNum,
					pageSize: this.pages.pageSize
				}
				examService.historyExam(param).then(response => {
					uni.stopPullDownRefresh();
					if (response.success) {
						this.pages.pageNum = response.result.pageNum;
						if (this.pages.pageNum == 1) {
							this.list = response.result.list;
							this.noMoreData = false;
						} else {
							this.noMoreData = response.result.list.length < param.pageSize
							this.list = [...this.list, ...response.result.list];
						}
					} else {
						uni.showToast({
							title: response.message,
							duration: 2000,
							icon: "none"
						})
					}
				}).then(() => {
					this.showSkeleton = false; //页面请求完成，取消骨架屏展示
				});
			},
			analysis(index,examId, recordId, finished) {
				this.curIndex = index;
				if (finished == 1) {
					uni.navigateTo({
						url: '/pages/exam/examsub?examType=5&examId=' + examId + '&recordId=' + recordId+'&resultType='+this.resultType,
					})
				} else {
					uni.navigateTo({
						// url:`/pages/exam/result?recordId=${recordId}`
						url: '/pages/exam/answerAnalysis?examId=' + examId + '&recordId=' + recordId + '&analyType=1' //1代表全部解析
					});
				}
			}
		}
	}
</script>

<style lang='scss' scoped>
	.container {
		background: #fff;
	}

	.list {
		margin: 0 20upx;

		&-item {
			display: flex;
			padding: 60upx 0 40upx;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1upx solid #f1f1f1;

			&-left {
				flex: 1;
				font-size: 28upx;
				color: #333;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			&-right {
				font-size: 20upx;
				color: #999;
				padding-left: 20upx;

				&-btn {
					border-radius: 8upx;
					border: 1upx solid #5ea1f8;
					font-size: 20upx;
					color: #5ea1f8;
					padding: 14upx 12upx;
				}
			}
		}
	}

	.loading {
		text-align: center;
		font-size: 28upx;
		padding: 20upx 0;
		color: #999;
	}

	.empty {
		margin-top: 150upx;
		font-size: 28upx;
		color: #666666;
		text-align: center;

		&-img {
			width: 220upx;
			height: 180upx;
		}

		&-txt {
			margin-top: 60upx;
		}
	}
</style>
